function Set() {
  this.items = {};

  Set.prototype.add = function (value) {
    if (this.has(value)) return false;
    this.items[value] = value;
    return true;
  };
  Set.prototype.remove = function (value) {
    // 先判断是否包含该元素
    if (!this.has(value)) return false;
    delete this.items[value];
    return true;
  };
  Set.prototype.has = function (value) {
    return this.items.hasOwnProperty(value);
  };
  Set.prototype.clear = function () {
    return (this.items = {});
  };
  Set.prototype.size = function () {
    return Object.keys(this.items).length;
  };
  Set.prototype.values = function () {
    return Object.keys(this.items);
  };

  //   *并集
  Set.prototype.union = (otherSet) => {
    // 1.创建并集变量
    let unionSet = new Set();
    // 2.将A集合中的所有元素添加到并集变量中
    let values = this.values();
    for (let i = 0; i < values.length; i++) {
      unionSet.add(values[i]);
    }
    // 3.取出B集合中的元素,判断是否有重复(已通过add方法实现)
    values = otherSet.values();
    for (let i = 0; i < values.length; i++) {
      unionSet.add(values[i]);
    }

    return unionSet;
  };

  //   交集intersection
  Set.prototype.intersection = function (otherSet) {
    /* 
        this:集合A
        ohterSet:集合B
    */
    //    1.创建新集合
    let intersectionSet = new Set();
    // 2.从A中循环取出元素,判断是否存在于集合B中,存放入新集合中
    let values = this.values();
    for (let i = 0; i < values.length; i++) {
      // 2.1 取出A集合中的元素
      let item = values[i];
      // 2.2 判断该元素是否存在于集合B
      if (otherSet.has(item)) {
        intersectionSet.add(item);
      }
    }
    return intersectionSet;
  };

  // 差集
  Set.prototype.difference = function (otherSet) {
    // 1.创建新集合
    let differenceSet = new Set();
    // 2.取出A集合中的元素,判断是否同时存在于B中,若不存在,则添加
    let values = this.values();
    for (let i = 0; i < values.length; i++) {
      let item = values[i];
      if (!otherSet.has(item)) {
        differenceSet.add(item);
      }
    }
    return differenceSet;
  };

  // 子集
  Set.prototype.subset = function (otherSet) {
    let values = this.values();
    for (let i = 0; i < values.length; i++) {
      let item = values[i];
      if (!otherSet.has(item)) return false;
    }
    return true;
  };
}

const setA = new Set();
console.log(setA.add('elysia'));
console.log(setA.add('seele'));
console.log(setA.add('kiana'));
console.log(setA);
console.log(setA.values().length);

const setB = new Set();
console.log(setB.add('elysia'));
console.log(setB.add('walter'));
console.log(setB.add('v2v'));
console.log(setB.add('19c'));
console.log(setB);

const setAB = setA.union(setB);
console.log('setA+setB', setAB);
console.log('setA U setB', setA.intersection(setB));
console.log('setA.difference(setB)', setA.difference(setB));
console.log(setA.subset(setB));
